<?php

class ZON_blog_widget extends WP_Widget {
	public function __construct() {
		parent::__construct( 'widget_blogs',
							 'Zonthemes - Widget Blog',
							 array( 'description' => __( 'Widget display blog in sidebar' ), ) 
		);
	}
	public function widget( $args, $instance ) {
		extract( $args );
		$title = apply_filters( 'title', $instance['title'] );
		$showposts = apply_filters( 'showposts', $instance['showposts'] );
		$layout = apply_filters( 'layout', $instance['layout'] );
		$cate = apply_filters( 'cate', $instance['cate'] );
		$args = array(
			'showposts' => $showposts,
			'post_type' => 'agents'
		);
		?>
		<div class="sidebar-box agents">
			<h2 class="sidebar-title"><?php echo $title; ?></h2>
			<ul class="<?php if($layout == 'right'){ echo 'right-images'; } ?>">
			<?php 
			$query = new WP_Query( $args);
			if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();?>
				<li>
					<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
						<img src="<?php echo zon_get_thumbnail('thumbnail'); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" />
					</a>
					<p class="sidebar-p-title"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
					<p><?php echo zon_meta('zon_agents_phone'); ?></p>
					<p><a href="mailto:<?php echo zon_meta('zon_agents_email'); ?>" title="<?php echo zon_meta('zon_agents_email'); ?>"><?php echo zon_meta('zon_agents_email'); ?></a></p>
					<div class="clear"></div>
				</li>
			<?php endwhile; endif; ?>
			</ul>
		</div>
		<?php
	}
	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['showposts'] = strip_tags( $new_instance['showposts'] );
		$instance['layout'] = strip_tags( $new_instance['layout'] );
		$instance['cate'] = strip_tags( $new_instance['cate'] );
		return $instance;
	}
	public function form( $instance ) {
		$defaults = array( 'title' =>__( 'Widget Agents' , 'tie'), 'showposts' =>__( '5' , 'tie' ) );
		$showposts = $instance['showposts']; 
		$title = $instance['title'];
		$layout = $instance['layout'];
		echo $cate = $instance['cate'];
		$cate_lists = get_categories(array('hide_empty'=>0));
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title :','zonthemes'); ?></label>
			<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'layout' ); ?>"><?php _e('Choose layout :','zonthemes'); ?></label>
			<select id="<?php echo $this->get_field_id( 'layout' ); ?>" name="<?php echo $this->get_field_name( 'layout' ); ?>">
			    <option value="left" <?php if($instance['layout'] == 'left'){ ?> selected="selected" <?php } ?>><?php _e('Images Left','zonthemes'); ?></option>
			    <option value="right" <?php if($instance['layout'] == 'right'){ ?> selected="selected" <?php } ?>><?php _e('Images right','zonthemes'); ?></option>
			</select>
		</p>
		<p>
			<label for="<?php echo $this->get_field_id( 'cate' ); ?>"><?php _e('Choose categories :','zonthemes'); ?></label>
			<select id="<?php echo $this->get_field_id( 'cate' ); ?>" name="<?php echo $this->get_field_name( 'cate' ); ?>">
			    <?php foreach($cate_lists as $cate_list){ ?>
					<option value="<?php $cate_list->term_id; ?>" <?php if($instance['cate'] == $cate_list->term_id){ ?> selected="selected" <?php } ?>><?php echo $cate_list->name; ?></option>
				<?php } ?>
			</select>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'showposts' ); ?>"><?php _e('Showposts :','zonthemes'); ?></label>
			<input id="<?php echo $this->get_field_id( 'showposts' ); ?>" name="<?php echo $this->get_field_name( 'showposts' ); ?>" value="<?php echo $instance['showposts']; ?>" class="widefat" type="text" />
		</p>
		<?php
	}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "ZON_blog_widget" );' ) );
